package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.baiju.bean.CityBean;
import com.baiju.bean.DistrictBean;
import com.baiju.bean.ProvinceBean;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AreaInfoController;
import com.example.dangerouscargodriver.controller.IControllerEventListener;
import com.example.dangerouscargodriver.entry.CityInfo;
import com.example.dangerouscargodriver.entry.DistrictInfo;
import com.example.dangerouscargodriver.entry.ProvinceInfo;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class ProvinceCityDistrictPickerActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "PickerActivity";

    public static final int AREA_SELECT_PROVINCE = 0;
    public static final int AREA_SELECT_CITY = 1;
    public static final int AREA_SELECT_CITY_WITH_ALL_PROVINCE = 2;
    public static final int AREA_SELECT_DISTRICT = 3;
    public static final int AREA_SELECT_DISTRICT_WITH_ALL_CITY = 4;

    private static final int COLUMN_NUM = 3;

    private int mSelectType = -1;
    private static final int MSG_REFRESH_DISTRICT = 1;
    private static final int MSG_REFRESH_CITY = 2;
    private static final int MSG_REFRESH_AREA_LOADED = 3;

    private ActionBar mActionBar = null;
    private ArrayList<ProvinceBean> mProvince;
    private int mCurrentProvinceIndex = -1;
    private List<CityBean> mCity;
    private int mCurrentCityIndex = -1;
    private List<DistrictBean> mAreas;
    private int mCurrentDistrictIndex = -1;
    private TableLayout mTableViewInterestingArea = null;
    private TextView mReturnPre = null;
    private int mShowContext = -1; //0 省级信息 1 市级信息 2 区级信息
    private LinearLayout mUserChooseArea = null;
    private LinearLayout mUserChooseItem = null;
    private Button mSelectBtn = null;
    private LinearLayout mUserCurrentItem = null;
    private MyControllerEventListener mControllerListener = null;
    private Handler mRefreshHandler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_province_city_district_picker);

        mTableViewInterestingArea = findViewById(R.id.table_view_interesting_place);
        mReturnPre = findViewById(R.id.btn_return_pre);
        mReturnPre.setOnClickListener(this);
        mShowContext = 0; //省级信息
        mUserChooseArea = findViewById(R.id.layout_user_choose_area);
        mUserChooseArea.setVisibility(View.GONE);
        mUserChooseItem = findViewById(R.id.layout_user_choose_item);
        mUserCurrentItem = findViewById(R.id.layout_user_current_item);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        if(getIntent().getParcelableExtra("province") == null &&
                getIntent().getParcelableExtra("city") == null &&
                getIntent().getParcelableExtra("district") == null){
            View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
            Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
            addBtn.setText("暂无");
            mUserCurrentItem.addView(btnLayout);
        }
        else{
            ProvinceBean province = (ProvinceBean) getIntent().getParcelableExtra("province");
            if(province != null){
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                addBtn.setText(province.getName());
                mUserCurrentItem.addView(btnLayout);
            }
            CityBean city = (CityBean) getIntent().getParcelableExtra("city");
            if(city != null){
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                addBtn.setText(city.getName());
                mUserCurrentItem.addView(btnLayout);
            }
            DistrictBean district = (DistrictBean) getIntent().getParcelableExtra("district");
            if(district != null){
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                addBtn.setText(district.getName());
                mUserCurrentItem.addView(btnLayout);
            }
        }
        mSelectBtn = findViewById(R.id.btn_select_area);
        mSelectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectType == AREA_SELECT_CITY || mSelectType == AREA_SELECT_CITY_WITH_ALL_PROVINCE) {
                    if (mCurrentCityIndex == -1) {
                        ToastUtils.showLongToast(getBaseContext(), "请选择具体的城市信息！");
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("city", mCity.get(mCurrentCityIndex));
                        intent.putExtra("province", mProvince.get(mCurrentProvinceIndex));

                        setResult(1, intent);
                        finish();
                    }
                }
                else if(mSelectType == AREA_SELECT_DISTRICT || mSelectType == AREA_SELECT_DISTRICT_WITH_ALL_CITY) {
                    if (mCurrentDistrictIndex == -1) {
                        ToastUtils.showLongToast(getBaseContext(), "请选择具体的区县信息！");
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("province", mProvince.get(mCurrentProvinceIndex));
                        intent.putExtra("city", mCity.get(mCurrentCityIndex));
                        intent.putExtra("district", mAreas.get(mCurrentDistrictIndex));

                        setResult(1, intent);
                        finish();
                    }
                }
                else{

                }
            }
        });

        mSelectType = getIntent().getIntExtra("type", -1);

        init();

        mActionBar = findViewById(R.id.actionBarCityPicker);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class MyHandler extends Handler{
        WeakReference<ProvinceCityDistrictPickerActivity> mActivity;

        MyHandler(ProvinceCityDistrictPickerActivity activity) {
            mActivity = new WeakReference<ProvinceCityDistrictPickerActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            ProvinceCityDistrictPickerActivity activity = mActivity.get();
            switch (msg.what){
                case MSG_REFRESH_DISTRICT:
                    activity.showArea();
                    break;
                case MSG_REFRESH_CITY:
                    activity.showCity();
                    break;
                case MSG_REFRESH_AREA_LOADED:
                    activity.showProvince();
                    break;
            }
        }
    }

    public void init() {
        mProvince = new ArrayList<>();
        mCity = new ArrayList<>();
        mAreas = new ArrayList<>();
        mRefreshHandler = new MyHandler(this);
        mControllerListener = new MyControllerEventListener();
        AreaInfoController.Instance().attachControllerListener(
                ProvinceCityDistrictPickerActivity.class,
                mControllerListener);
    }

    public void showProvince() {
        //在加载地址信息异常或未加载完毕的情况下，不显示任何信息
        List<ProvinceInfo> provinceInfo = AreaInfoController.Instance().getProvinceInfo();
        if(provinceInfo == null || provinceInfo.size() == 0){
            return;
        }

        for(int index = 0; index < provinceInfo.size(); index++){
            ProvinceBean bean = new ProvinceBean();
            bean.setName(provinceInfo.get(index).getName());
            bean.setId(provinceInfo.get(index).getId());
            mProvince.add(bean);
        }

        int provinceNum = mProvince.size();
        int needNum = 0;
        int rowNum = provinceNum / COLUMN_NUM;

        if(provinceNum % COLUMN_NUM != 0){
            needNum = provinceNum % COLUMN_NUM;
        }
        rowNum += 1;

        mTableViewInterestingArea.removeAllViews();
        mShowContext = 0;
        mReturnPre.setVisibility(View.GONE);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        for(int rowIndex = 0; rowIndex < rowNum - 1; rowIndex++){
            TableRow row = new TableRow(this);
            for(int colIndex = 0; colIndex < COLUMN_NUM; colIndex++) {
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                //addBtn.setBackgroundColor(Color.WHITE);
                final int current = rowIndex * COLUMN_NUM + colIndex;
                if(mCurrentProvinceIndex == current){
                    addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                }
                addBtn.setText(mProvince.get(current).getName());
                addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button clickBtn = (Button)v;
                        //将tablelayout初始化到省对应的市选择
                        if(mCurrentProvinceIndex == current){
                            mCurrentProvinceIndex = -1;
                            mCurrentCityIndex = -1;
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            mUserChooseArea.setVisibility(View.GONE);
                        }
                        else {
                            clickBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                            mCurrentProvinceIndex = current;
                            mCurrentCityIndex = -1;
                            mCurrentDistrictIndex = -1;

                            if(mSelectType < AREA_SELECT_CITY){
                                return;
                            }

                            ProvinceBean provinceBean = mProvince.get(current);
                            List<CityInfo> cityInfos = AreaInfoController.Instance().getCityInfo(provinceBean.getId());
                            if(cityInfos == null || cityInfos.size() == 0){
                                return;
                            }

                            mCity.clear();
                            for(int index = 0; index < cityInfos.size(); index++){
                                CityBean bean = new CityBean();
                                bean.setName(cityInfos.get(index).getExtra().getCity().getName());
                                bean.setId(cityInfos.get(index).getExtra().getCity().getId());
                                mCity.add(bean);
                            }

                            if(mSelectType == AREA_SELECT_CITY_WITH_ALL_PROVINCE) {
                                CityBean allProvince = new CityBean();
                                allProvince.setName("全省");
                                allProvince.setId("0");
                                mCity.add(0, allProvince);
                            }

                            if (mCity == null) {
                                return;
                            } else {
                                showCity();
                                mUserChooseArea.setVisibility(View.VISIBLE);
                                mUserChooseItem.removeAllViews();
                                View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                                TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                                chooseView.setText(provinceBean.getName());
                                mUserChooseItem.addView(userChooseLayout);
                            }
                        }
                    }
                });
                row.addView(btnLayout);
            }
            mTableViewInterestingArea.addView(row);
        }

        if(needNum == 0){
            return;
        }

        TableRow row = new TableRow(this);
        for(int colIndex = 0; colIndex < COLUMN_NUM; colIndex++) {
            final int current = (rowNum - 1) * COLUMN_NUM + colIndex;
            if(colIndex < needNum) {
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                if(mCurrentProvinceIndex == current){
                    addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                }
                addBtn.setText(mProvince.get(current).getName());
                addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button clickBtn = (Button) v;
                        //将tablelayout初始化到省对应的市选择
                        if (mCurrentProvinceIndex == current) {
                            mCurrentProvinceIndex = -1;
                            mCurrentCityIndex = -1;
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            mUserChooseArea.setVisibility(View.GONE);
                        } else {
                            clickBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                            mCurrentProvinceIndex = current;
                            mCurrentCityIndex = -1;
                            mCurrentDistrictIndex = -1;
                            if (mSelectType < AREA_SELECT_CITY) {
                                return;
                            }

                            ProvinceBean provinceBean = mProvince.get(current);
                            List<CityInfo> cityInfos = AreaInfoController.Instance().getCityInfo(provinceBean.getId());
                            if (cityInfos == null || cityInfos.size() == 0) {
                                return;
                            }
                            mCity.clear();
                            for (int index = 0; index < cityInfos.size(); index++) {
                                CityBean bean = new CityBean();
                                bean.setName(cityInfos.get(index).getExtra().getCity().getName());
                                bean.setId(cityInfos.get(index).getExtra().getCity().getId());
                                mCity.add(bean);
                            }
                            if (mSelectType == AREA_SELECT_CITY_WITH_ALL_PROVINCE) {
                                CityBean allProvince = new CityBean();
                                allProvince.setName("全省");
                                allProvince.setId("0");
                                mCity.add(0, allProvince);
                            }

                            if (mCity == null) {
                                return;
                            } else {
                                showCity();
                                mUserChooseArea.setVisibility(View.VISIBLE);
                                mUserChooseItem.removeAllViews();
                                View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                                TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                                chooseView.setText(provinceBean.getName());
                                mUserChooseItem.addView(userChooseLayout);
                            }
                        }
                    }
                });
                row.addView(btnLayout);
            }
            else{
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.empty_multi_item, null);
                row.addView(btnLayout);
            }
        }
        mTableViewInterestingArea.addView(row);
    }

    public void showCity() {
        int cityNum = mCity.size();
        int needNum = 0;
        int rowNum = cityNum / COLUMN_NUM;

        if(cityNum % COLUMN_NUM != 0){
            needNum = cityNum % COLUMN_NUM;
        }
        rowNum += 1;

        mTableViewInterestingArea.removeAllViews();
        mShowContext = 1;
        mReturnPre.setVisibility(View.VISIBLE);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        for(int rowIndex = 0; rowIndex < rowNum - 1; rowIndex++){
            TableRow row = new TableRow(this);
            for(int colIndex = 0; colIndex < COLUMN_NUM; colIndex++) {
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                final int current = rowIndex * COLUMN_NUM + colIndex;
                addBtn.setText(mCity.get(current).getName());
                if(mCurrentCityIndex == current){
                    addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                }
                addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button clickBtn = (Button)v;
                        if(mCurrentCityIndex == current){
                            mCurrentCityIndex = -1;
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            for(int index = mUserChooseItem.getChildCount(); index > 1; index--){
                                mUserChooseItem.removeViewAt(index -1);
                            }
                        }
                        else {
                            //将tablelayout初始化到省对应的市选择
                            //省份选中的名称
                            CityBean cityBean = mCity.get(current);
                            mCurrentCityIndex = current;
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#fffff18f"));

                            if(mSelectType < AREA_SELECT_DISTRICT){
                                if(mUserChooseItem.getChildCount() == 2) {
                                    mUserChooseItem.removeViewAt(1);
                                }
                                View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                                TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                                chooseView.setText(cityBean.getName());
                                mUserChooseItem.addView(userChooseLayout);

                                Message msg = new Message();
                                msg.what = MSG_REFRESH_CITY;
                                mRefreshHandler.sendMessage(msg);
                                return;
                            }

                            List<DistrictInfo> districtInfos = AreaInfoController.Instance().getDistrictInfo(
                                    mProvince.get(mCurrentProvinceIndex).getId(),
                                    cityBean.getId()
                            );
                            if(districtInfos == null || districtInfos.size() == 0){
                                return;
                            }
                            mAreas.clear();
                            for(int index = 0; index < districtInfos.size(); index++){
                                DistrictBean districtBean = new DistrictBean();
                                districtBean.setId(districtInfos.get(index).getExtra().getTown().getId());
                                districtBean.setName(districtInfos.get(index).getExtra().getTown().getName());
                                mAreas.add(districtBean);
                            }
                            if(mSelectType == AREA_SELECT_DISTRICT_WITH_ALL_CITY) {
                                DistrictBean allCity = new DistrictBean();
                                allCity.setName("全市");
                                allCity.setId("0");
                                mAreas.add(0, allCity);
                            }

                            if (mAreas == null || mAreas.size() == 0) {
                                return;
                            }
                            else {
                                showArea();
                                for(int index = mUserChooseItem.getChildCount(); index > 1; index--){
                                    mUserChooseItem.removeViewAt(index -1);
                                }
                                View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                                TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                                chooseView.setText(cityBean.getName());
                                mUserChooseItem.addView(userChooseLayout);
                            }
                        }
                    }
                });
                row.addView(btnLayout);
            }
            mTableViewInterestingArea.addView(row);
        }

        if(needNum == 0){
            return;
        }

        TableRow row = new TableRow(this);
        for(int colIndex = 0; colIndex < COLUMN_NUM; colIndex++) {
            final int current = (rowNum - 1) * COLUMN_NUM + colIndex;
            if(colIndex < needNum) {
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                addBtn.setText(mCity.get(current).getName());
                if (mCurrentCityIndex == current) {
                    addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                }
                addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button clickBtn = (Button) v;
                        if (mCurrentCityIndex == current) {
                            mCurrentCityIndex = -1;
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            for (int index = mUserChooseItem.getChildCount(); index > 1; index--) {
                                mUserChooseItem.removeViewAt(index - 1);
                            }
                        } else {
                            //将tablelayout初始化到省对应的市选择
                            //省份选中的名称
                            CityBean cityBean = mCity.get(current);
                            mCurrentCityIndex = current;
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#fffff18f"));

                            if (mSelectType < AREA_SELECT_DISTRICT) {
                                if (mUserChooseItem.getChildCount() == 2) {
                                    mUserChooseItem.removeViewAt(1);
                                }
                                View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                                TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                                chooseView.setText(cityBean.getName());
                                mUserChooseItem.addView(userChooseLayout);

                                Message msg = new Message();
                                msg.what = MSG_REFRESH_CITY;
                                mRefreshHandler.sendMessage(msg);

                                return;
                            }

                            List<DistrictInfo> districtInfos = AreaInfoController.Instance().getDistrictInfo(
                                    mProvince.get(mCurrentProvinceIndex).getId(),
                                    cityBean.getId()
                            );

                            if (districtInfos == null || districtInfos.size() == 0) {
                                return;
                            }
                            mAreas.clear();

                            for (int index = 0; index < districtInfos.size(); index++) {
                                DistrictBean districtBean = new DistrictBean();
                                districtBean.setId(districtInfos.get(index).getExtra().getTown().getId());
                                districtBean.setName(districtInfos.get(index).getExtra().getTown().getName());
                                mAreas.add(districtBean);
                            }
                            if (mSelectType == AREA_SELECT_DISTRICT_WITH_ALL_CITY) {
                                DistrictBean allCity = new DistrictBean();
                                allCity.setName("全市");
                                allCity.setId("0");
                                mAreas.add(0, allCity);
                            }

                            if (mAreas == null || mAreas.size() == 0) {
                                return;
                            } else {
                                showArea();
                                for (int index = mUserChooseItem.getChildCount(); index > 1; index--) {
                                    mUserChooseItem.removeViewAt(index - 1);
                                }
                                View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                                TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                                chooseView.setText(cityBean.getName());
                                mUserChooseItem.addView(userChooseLayout);
                            }
                        }
                    }
                });
                row.addView(btnLayout);
            }
            else{
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.empty_multi_item, null);
                row.addView(btnLayout);
            }
        }
        mTableViewInterestingArea.addView(row);
    }

    public void showArea() {
        int areaNum = mAreas.size();
        int needNum = 0;
        int rowNum = areaNum / COLUMN_NUM;
        if(areaNum % COLUMN_NUM != 0){
            needNum = areaNum % COLUMN_NUM;
        }
        rowNum += 1;

        mTableViewInterestingArea.removeAllViews();
        mShowContext = 2;
        mReturnPre.setVisibility(View.VISIBLE);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        for(int rowIndex = 0; rowIndex < rowNum - 1; rowIndex++){
            TableRow row = new TableRow(this);
            for(int colIndex = 0; colIndex < COLUMN_NUM; colIndex++) {
                View btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                final int current = rowIndex * COLUMN_NUM + colIndex;
                addBtn.setText(mAreas.get(current).getName());
                if(mCurrentDistrictIndex == current){
                    addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                }
                addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button clickBtn = (Button)v;
                        if(mCurrentDistrictIndex == current){
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            for(int index = mUserChooseItem.getChildCount(); index > 2; index--){
                                mUserChooseItem.removeViewAt(index -1);
                            }
                        }
                        else {
                            addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                            DistrictBean districtBean = mAreas.get(current);
                            mCurrentDistrictIndex = current;
                            for(int index = mUserChooseItem.getChildCount(); index > 2; index--){
                                mUserChooseItem.removeViewAt(index -1);
                            }
                            View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                            TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                            chooseView.setText(districtBean.getName());
                            mUserChooseItem.addView(userChooseLayout);
                            Message msg = new Message();
                            msg.what = MSG_REFRESH_DISTRICT;
                            mRefreshHandler.sendMessage(msg);
                        }
                    }
                });
                row.addView(btnLayout);
            }
            mTableViewInterestingArea.addView(row);
        }

        if(needNum == 0){
            return;
        }
        TableRow row = new TableRow(this);
        for(int colIndex = 0; colIndex < COLUMN_NUM; colIndex++) {
            final int current = (rowNum - 1) * COLUMN_NUM + colIndex;
            View btnLayout = null;
            if(colIndex < needNum) {
                btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.default_multi_item, null);
                Button addBtn = btnLayout.findViewById(com.baiju.style.citypickerview.R.id.default_multi_item_name_tv);
                if (mCurrentDistrictIndex == current) {
                    addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                }
                addBtn.setText(mAreas.get(current).getName());
                addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button clickBtn = (Button) v;
                        if (mCurrentDistrictIndex == current) {
                            mCurrentDistrictIndex = -1;
                            clickBtn.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            for (int index = mUserChooseItem.getChildCount(); index > 2; index--) {
                                mUserChooseItem.removeViewAt(index - 1);
                            }
                        } else {
                            addBtn.setBackgroundColor(Color.parseColor("#fffff18f"));
                            DistrictBean districtBean = mAreas.get(current);
                            mTableViewInterestingArea.getChildAt(mCurrentCityIndex).setBackgroundColor(Color.parseColor("#F5F5F5"));
                            mCurrentDistrictIndex = current;
                            for (int index = mUserChooseItem.getChildCount(); index > 2; index--) {
                                mUserChooseItem.removeViewAt(index - 1);
                            }
                            View userChooseLayout = layoutInflater.inflate(R.layout.item_choosed_city, null);
                            TextView chooseView = userChooseLayout.findViewById(R.id.btn_user_choose_item);
                            chooseView.setText(districtBean.getName());
                            mUserChooseItem.addView(userChooseLayout);
                        }
                    }
                });
            }
            else{
                btnLayout = layoutInflater.inflate(com.baiju.style.citypickerview.R.layout.empty_multi_item, null);
            }
            row.addView(btnLayout);
        }
        mTableViewInterestingArea.addView(row);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_return_pre){
            if(mShowContext == 2){
                showCity();
            }
            else if(mShowContext == 1){
                showProvince();
            }
            else{

            }
        }
        else{

        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        AreaInfoController.Instance().unAttachControllerListener(
                ProvinceCityDistrictPickerActivity.class,
                mControllerListener);
    }

    private class MyControllerEventListener implements IControllerEventListener {

        @Override
        public void onEvent(int eventId, String json) {
            if(eventId == Event.EVENT_LOAD_HTTP_RESOURCE_AREA){
                Gson gson = new Gson();
                LoadHttpResourceStatus status = gson.fromJson(json, LoadHttpResourceStatus.class);
                Log.d(TAG, "onEvent: " + status.toJson());
                if(status.getWhat() == AreaInfoController.EVENT_AREA_LOAD_DONE){
                    Message msg = new Message();
                    msg.what = MSG_REFRESH_AREA_LOADED;
                    mRefreshHandler.sendMessage(msg);
                }
            }
        }
    }
}